package cn.yuanqiao.quartz.task;

import cn.yuanqiao.archive.Identify.service.IBgqxIdentifyService;
import cn.yuanqiao.archive.xiaoxitongzhi.service.IYqXxtzService;
import cn.yuanqiao.common.core.domain.entity.SysUser;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.quartz.util.MethodDescription;
import cn.yuanqiao.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 磁盘空间预警
 */
@Component("warningInformationTask")
public class WarningInformationTask {
    private static final Logger log = LoggerFactory.getLogger(WarningInformationTask.class);

    @Autowired
    private IYqXxtzService archiveReleaseService;
    @Autowired
    private ISysUserService userService;
    @Value("${archiveLogPath}")
    private String logPath;
    @Autowired
    private IBgqxIdentifyService bgqxIdentifyService;

    /**
     * 磁盘空间预警
     */
    @MethodDescription("磁盘空间告警定时任务，arg0需要传入tenantCode")
    public void diskInformation(String tenantCode){
        try {
            if(StringUtils.isNotEmpty(tenantCode)){
                File logsFile = new File(logPath);
                if(logsFile.exists()){
                    //获取磁盘总大小
                    long totalSpace = logsFile.getTotalSpace();
                    //获取磁盘可用空间大小
                    long freeSpace = logsFile.getFreeSpace();
                    //剩余空间比例
                    double xy = (double) freeSpace / totalSpace;

                    log.info("磁盘路径={}",logsFile);
                    log.info("磁盘总大小={}",totalSpace/1024/1024/1024);
                    log.info("可用空间大小={}",freeSpace/1024/1024/1024);
                    log.info("剩余空间比例={}",xy);

                    if(xy<0.2){ //可以空间小于20%
                        String msg = "您的档案系统磁盘存储空间不足20%，请尽快处理！";
                        SysUser sysadminUser = userService.selectUserByUserName("sysadmin",tenantCode); //消息创建人
                        SysUser sysssoUser = userService.selectUserByUserName("syssso",tenantCode);  //消息接收人
                        archiveReleaseService.addyqXxtzApi(msg,sysadminUser.getUserId(),sysssoUser.getUserId(),tenantCode);
                    }
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }

    }

    /**
     * bgqx到期提醒
     */
    @MethodDescription("保管期限定时任务，arg0需要传入tenantCode")
    public void bgqxdqtx(String tenantCode){
        try {
            if(StringUtils.isNotEmpty(tenantCode)){
                List<Long> daglyusers=userService.getdaglyuser(tenantCode);
                //开放鉴定到期提醒
                Map<String,Object> getisopendqda = bgqxIdentifyService.getisopendqda(tenantCode);
                //List<String> detail1 = (List<String>) getisopendqda.get("detail");
                String msg1 = "[开放鉴定到期提醒]"+(String) getisopendqda.get("msg");
                //for (int i = 0; i < detail.size(); i++) {
                //archiveReleaseService.addXxtz(detail.get(i),-1L,-1L,tenantCode);
                //}
                if(null!=msg1&&!"".equals(msg1)){
                    for (int i = 0; i < daglyusers.size(); i++) {
                        archiveReleaseService.addyqXxtzApi(msg1,-1L,daglyusers.get(i),tenantCode);
                    }
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }

    }
}
